package com.book.activeMQ.consumers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.book.dto.BookDto;
import com.book.service.BookService;
import com.book.util.Constants;
import com.book.util.EhCacheManager;
import com.book.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/17 9:49 下午
 * @Project_Name graduation-project
 */
@Slf4j
@Component
public class UpDateNewBookRedis implements MessageListener {
    @Autowired
    private BookService uBookService;
    @Autowired
    private EhCacheManager ehCacheManager;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            String text = textMessage.getText();
            List object = JSONObject.parseObject(text,List.class);
            log.info("UpDateNewBookRedis#object:{}",object);
            List<BookDto> list = (List<BookDto>) object.stream().map(m->JSONObject.toJavaObject((JSON) m, BookDto.class)).collect(Collectors.toList());
            log.info("UpDateNewBookRedis#list:{}",list);
            for (BookDto b:list
            ) {
                redisUtil.rPush(Constants.NEW_BOOK,b);
            }
        } catch (Exception e) {
            log.error("UpDateNewBookRedis异常",e);
            e.printStackTrace();
        }
    }
}
